package com.learn.springboot.controller.database.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author wangxing
 * @version 2020/8/4 10:35 Administrator
 */
@RestController
@RequestMapping("/redis")
public class RedisController {
     Logger logger = LoggerFactory.getLogger(RedisController.class);
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @PutMapping("/set/string/{key}_{value}")
    public String setStringData(@PathVariable("key") String key,
                                @PathVariable("value") String value){
        stringRedisTemplate.opsForValue().set(key, value);
        return "success";
    }
    @GetMapping("/get/string/{key}")
    public String getStringData(@PathVariable("key") String key){
        return stringRedisTemplate.opsForValue().get(key);
    }
    @PutMapping("/set/string/time/{key}_{value}_{time}")
    public String setKeyTime(@PathVariable("key") String key,@PathVariable("value") String value,
                             @PathVariable("time") Long time){
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
        return "success";
    }
    @GetMapping("/get/string/time/{key}")
    public String getKeyTime(@PathVariable("key") String key){
        return redisTemplate.opsForValue().get(key).toString();
    }


}
